package com.link4a.doms.dao;

import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * @author brown
 */
public final class NamespaceSqlSession implements SqlSession {

	private final SqlSession sqlSession;
	private final String namespace;

	public NamespaceSqlSession(SqlSession sqlSession, String namespace) {
		this.sqlSession = sqlSession;
		this.namespace = namespace;
	}

	private final String toFullId(String id) {
		return namespace + "." + id;
	}

	public SqlSession getRawSqlSession() {
		return sqlSession;
	}

	@Override
	public <T> T selectOne(String statement) {
		return sqlSession.selectOne(toFullId(statement));
	}

	@Override
	public <T> T selectOne(String statement, Object parameter) {
		return sqlSession.selectOne(toFullId(statement), parameter);
	}

	@Override
	public <E> List<E> selectList(String statement) {
		return sqlSession.selectList(toFullId(statement));
	}

	@Override
	public <E> List<E> selectList(String statement, Object parameter) {
		return sqlSession.selectList(toFullId(statement), parameter);
	}

	@Override
	public <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds) {
		return sqlSession.selectList(toFullId(statement), parameter, rowBounds);
	}

	@Override
	public <K, V> Map<K, V> selectMap(String statement, String mapKey) {
		return sqlSession.selectMap(toFullId(statement), mapKey);
	}

	@Override
	public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey) {
		return sqlSession.selectMap(toFullId(statement), parameter, mapKey);
	}

	@Override
	public <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
		return sqlSession.selectMap(toFullId(statement), parameter, mapKey, rowBounds);
	}

	@Override
	public void select(String statement, Object parameter, ResultHandler handler) {
		sqlSession.select(toFullId(statement), parameter, handler);
	}

	@Override
	public void select(String statement, ResultHandler handler) {
		sqlSession.select(toFullId(statement), handler);
	}

	@Override
	public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
		sqlSession.select(toFullId(statement), parameter, rowBounds, handler);
	}

	@Override
	public int insert(String statement) {
		return sqlSession.insert(toFullId(statement));
	}

	@Override
	public int insert(String statement, Object parameter) {
		return sqlSession.insert(toFullId(statement), parameter);
	}

	@Override
	public int update(String statement) {
		return sqlSession.update(toFullId(statement));
	}

	@Override
	public int update(String statement, Object parameter) {
		return sqlSession.update(toFullId(statement), parameter);
	}

	@Override
	public int delete(String statement) {
		return sqlSession.delete(toFullId(statement));
	}

	@Override
	public int delete(String statement, Object parameter) {
		return sqlSession.delete(toFullId(statement), parameter);
	}

	@Override
	public void commit() {
		sqlSession.commit();
	}

	@Override
	public void commit(boolean force) {
		sqlSession.commit(force);
	}

	@Override
	public void rollback() {
		sqlSession.rollback();
	}

	@Override
	public void rollback(boolean force) {
		sqlSession.rollback(force);
	}

	@Override
	public List<BatchResult> flushStatements() {
		return sqlSession.flushStatements();
	}

	@Override
	public void close() {
		sqlSession.close();
	}

	@Override
	public void clearCache() {
		sqlSession.clearCache();
	}

	@Override
	public Configuration getConfiguration() {
		return sqlSession.getConfiguration();
	}

	@Override
	public <T> T getMapper(Class<T> type) {
		return sqlSession.getMapper(type);
	}

	@Override
	public Connection getConnection() {
		return sqlSession.getConnection();
	}

}
